﻿@using Example3.InvoiceCode.Services
@using Example3.MvcWebApp.IndividualAccounts.PermissionsCode
@using AuthPermissions.BaseCode.PermissionsCode
@model List<Example3.InvoiceCode.Dtos.InvoiceSummaryDto>

@{
    ViewData["Title"] = "Index";
    Layout = "_CompanyLayout";
}

@if (!string.IsNullOrEmpty(ViewBag.Message))
{
    <h3 class="text-success">@ViewBag.Message</h3>
}

<h1>@AddTenantNameClaim.GetTenantNameFromUser(User) invoices</h1>
<p>
    <i>NOTE: This example multi-tenant application makes each company think the application was build just for them.</i>
</p>

@if (User.HasPermission(Example3Permissions.InvoiceCreate))
{
    <p>
        <a class="btn btn-primary" asp-action="CreateInvoice">Create Invoice</a>
    </p>
}
else
{
    <h4>You can see the list of current invoices, but you can't create a new invoice.</h4>
}

@if (Model != null)
{
    <table class="table">
        <thead>
        <tr>
            <th>
                InvoiceName
            </th>
            <th>
                DateCreated
            </th>
            <th>
                NumItems
            </th>
            <th>
                TotalCost
            </th>
            @*<th></th>*@
        </tr>
        </thead>
        <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.InvoiceName)
                </td>
                <td>
                    @item.DateCreated.ToString("d")
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.NumItems)
                </td>
                <td>
                    @item.TotalCost?.ToString("C2")
                </td>
            </tr>
        }
        </tbody>
    </table>

    @if (User.HasPermission(Example3Permissions.InvoiceSum))
    {
        <h2>Enterprise Feature!</h2>
        @if (Model.Any())
        {
            <div>The sum of the @Model.Count invoices is <span class="h3">@Model.Sum(x => x.TotalCost)?.ToString("C2")</span></div>
        }
        else
        {
            <div>No invoices found</div>
        }
    }
    else
    {
        <div class="h4">If you upgrade to the Enterprise version you will see total cost of your invoices.</div>
    }
}
else
{
    <h4>You can create a invoice, but you can't see the list of current invoices.</h4>
}